package com.klm.easymq.controller;

import com.klm.easymq.service.RocketMQProducerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Easy-MQ RocketMQ测试控制器
 * 提供简单的测试接口
 */
@Slf4j
@RestController
@RequestMapping("/easymq/api/rocketmq/test")
public class EasyMqRocketMQTestController {
    
    @Autowired
    private RocketMQProducerService rocketMQProducerService;
    
    /**
     * 测试发送同步消息到默认主题
     */
    @PostMapping("/send-default-topic")
    public Map<String, Object> sendToDefaultTopic(@RequestBody Map<String, String> request) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            String message = request.get("message");
            if (message == null) {
                message = "Hello RocketMQ! " + System.currentTimeMillis();
            }
            
            // 发送到默认主题
            String topic = "easy-mq-topic";
            boolean result = rocketMQProducerService.sendSyncMessage(topic, message);
            
            response.put("success", result);
            response.put("message", result ? "消息发送成功" : "消息发送失败");
            response.put("topic", topic);
            response.put("messageContent", message);
            response.put("timestamp", System.currentTimeMillis());
            
        } catch (Exception e) {
            log.error("测试发送消息异常", e);
            response.put("success", false);
            response.put("message", "测试发送消息异常: " + e.getMessage());
        }
        
        return response;
    }
    
    /**
     * 测试发送延迟消息
     */
    @PostMapping("/send-delay")
    public Map<String, Object> sendDelayMessage(@RequestBody Map<String, Object> request) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            String message = (String) request.get("message");
            Integer delayLevel = (Integer) request.get("delayLevel");
            
            if (message == null) {
                message = "延迟消息测试! " + System.currentTimeMillis();
            }
            if (delayLevel == null) {
                // 10秒延迟
                delayLevel = 3;
            }
            
            String topic = "easy-mq-topic";
            boolean result = rocketMQProducerService.sendDelayMessage(topic, message, delayLevel);
            
            response.put("success", result);
            response.put("message", result ? "延迟消息发送成功" : "延迟消息发送失败");
            response.put("topic", topic);
            response.put("messageContent", message);
            response.put("delayLevel", delayLevel);
            response.put("timestamp", System.currentTimeMillis());
            
        } catch (Exception e) {
            log.error("测试发送延迟消息异常", e);
            response.put("success", false);
            response.put("message", "测试发送延迟消息异常: " + e.getMessage());
        }
        
        return response;
    }
    
    /**
     * 测试发送顺序消息
     */
    @PostMapping("/send-ordered")
    public Map<String, Object> sendOrderedMessage(@RequestBody Map<String, String> request) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            String message = request.get("message");
            String hashKey = request.get("hashKey");
            
            if (message == null) {
                message = "顺序消息测试! " + System.currentTimeMillis();
            }
            if (hashKey == null) {
                hashKey = "test-key";
            }
            
            String topic = "easy-mq-topic";
            boolean result = rocketMQProducerService.sendOrderedMessage(topic, message, hashKey);
            
            response.put("success", result);
            response.put("message", result ? "顺序消息发送成功" : "顺序消息发送失败");
            response.put("topic", topic);
            response.put("messageContent", message);
            response.put("hashKey", hashKey);
            response.put("timestamp", System.currentTimeMillis());
            
        } catch (Exception e) {
            log.error("测试发送顺序消息异常", e);
            response.put("success", false);
            response.put("message", "测试发送顺序消息异常: " + e.getMessage());
        }
        
        return response;
    }
    
    /**
     * 批量发送测试消息
     */
    @PostMapping("/batch-send")
    public Map<String, Object> batchSend(@RequestBody Map<String, Object> request) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            Integer count = (Integer) request.get("count");
            if (count == null || count <= 0) {
                count = 10;
            }
            
            String topic = "easy-mq-topic";
            int successCount = 0;
            int failCount = 0;
            
            for (int i = 0; i < count; i++) {
                String message = "批量消息测试 #" + (i + 1) + " - " + System.currentTimeMillis();
                boolean result = rocketMQProducerService.sendSyncMessage(topic, message);
                if (result) {
                    successCount++;
                } else {
                    failCount++;
                }
                
                // 稍微延迟一下，避免发送过快
                Thread.sleep(100);
            }
            
            response.put("success", true);
            response.put("message", "批量发送完成");
            response.put("topic", topic);
            response.put("totalCount", count);
            response.put("successCount", successCount);
            response.put("failCount", failCount);
            response.put("timestamp", System.currentTimeMillis());
            
        } catch (Exception e) {
            log.error("批量发送测试消息异常", e);
            response.put("success", false);
            response.put("message", "批量发送测试消息异常: " + e.getMessage());
        }
        
        return response;
    }
} 